import Utils from "../../../../../Utils";
import { FacilityType } from "../../../../info/GameEnum";
import { Location, MapLength } from "../../../../info/Predefine";
import { AiEnvironment } from "../../AiGameAction";
import { DecisionNode } from "../../DecisionTree";

/**
 * 发射视野飞行物
 */
export default class DecisionViewDispatch extends DecisionNode {

    environment: AiEnvironment & { scoutCount: number, flareCount: number, dispatchLocation: Location };
    scoutCount: number;
    flareCount: number;

    constructor(environment: AiEnvironment) {
        super(environment);
        this.childDecisionNodes.push(new DecisionDispatchScout(environment))
        this.childDecisionNodes.push(new DecisionDispatchFlare(environment))
    }

    evaluate(): number {
        this.scoutCount = this.environment.player.scoutCount;
        this.flareCount = this.environment.player.flareCount;
        return (this.scoutCount + this.flareCount) * 80;
    }

    execute(): number {
        this.environment.scoutCount = this.scoutCount;
        this.environment.flareCount = this.flareCount;

        //确定需要发射飞行物的位置
        let randomFacLocation: Location;
        if (this.environment.invisibilityAttackedFacility.length) {
            //距离主基地越近 得分越高
            let mainhomeLocation = Utils.getRandomChoose(this.environment.player.facilityArr[FacilityType.主基地][0].locations);
            let scores: number[] = this.environment.invisibilityAttackedFacility.map(facility => {
                let location = facility.locations[0];
                let disX = Math.abs(mainhomeLocation.locationX - location.locationX),
                    disY = Math.abs(mainhomeLocation.locationY - location.locationY)

                let distance = Math.min(100, Math.sqrt(disX * disX + disY * disY));
                //最低1分 最高 100
                return Math.ceil(101 - distance) ;
            })
            let randomIndex = Utils.getRandomIndexByProbability(scores);
            if(!this.environment.invisibilityAttackedFacility[randomIndex]){
                cc.error("error");
            }
            randomFacLocation = Utils.getRandomChoose(this.environment.invisibilityAttackedFacility[randomIndex].locations);
        } else if (this.environment.invisibilityFacility.length) {
            //随机一个不可见的敌方设施
            let randomFac = Utils.getRandomChoose(this.environment.invisibilityFacility)
            randomFacLocation = Utils.getRandomChoose(randomFac.locations);
        } else {
            //随机一个位置
            randomFacLocation = { locationX: Utils.getRandom(0, MapLength), locationY: Utils.getRandom(0, MapLength) };
        }
        let minx = Math.max(0, randomFacLocation.locationX - 5),
            maxX = Math.min(MapLength - 1, randomFacLocation.locationX + 5),
            minY = Math.max(0, randomFacLocation.locationY - 5),
            maxY = Math.min(MapLength - 1, randomFacLocation.locationY + 5);
        this.environment.dispatchLocation = { locationX: Utils.getRandom(minx, maxX), locationY: Utils.getRandom(minY, maxY) };

        return super.execute();
    }
}

/**
 * 发射侦察机
 */
class DecisionDispatchScout extends DecisionNode {
    environment: AiEnvironment & { scoutCount: number, flareCount: number, dispatchLocation: Location };

    evaluate(): number {
        return Math.min(100, this.environment.player.scoutCount * 20);
    }

    execute(): number {
        //优先探查不再视野内攻击我的设施
        this.environment.player.dispatchFlyer(FacilityType.侦察机基地, this.environment.dispatchLocation);
        return 0;
    }
}

/**
 * 发射照明弹
 */
class DecisionDispatchFlare extends DecisionNode {
    environment: AiEnvironment & { scoutCount: number, flareCount: number, dispatchLocation: Location };

    evaluate(): number {
        return Math.min(100, this.environment.player.flareCount * 50);
    }

    execute(): number {
        this.environment.player.dispatchFlyer(FacilityType.照明弹基地, this.environment.dispatchLocation);
        return 0;
    }
}